package com.xander.myblog.controller;

import com.xander.myblog.bean.ResultBean;
import com.xander.myblog.entity.Comment;
import com.xander.myblog.entity.User;
import com.xander.myblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author hbxz
 * @date 2018/12/20 0020
 */
@Controller
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("addComment")
    @ResponseBody
    public ResultBean addComment(Comment comment, HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user!=null){
            comment.setNickname(user.getNickname());
            comment.setEmail(user.getEmail());
            comment.setAuthorId(user.getUserId());
            comment.setCreator(user.getNickname());
        }else {
           comment.setCreator("游客："+comment.getNickname());
        }
        commentService.insertComment(comment);
        return new ResultBean("评论成功");
    }
}
